<?php

function get_paradise_list($con, $keyword = NULL, $order_by = NULL, $order)
{
	$query = "SELECT * FROM paradise";

	/*
	if ($with_picture) {
		$query = "SELECT paradise.*, picture.thumb_small " .
                	"FROM paradise, picture WHERE paradise.picture_id = picture.id ORDER BY id DESC";
	}
	*/
	
	if ($keyword != NULL) {
		$query .= " WHERE name LIKE '%$keyword%'";
	}
	
	if ($order_by != NULL) {
		$query .= " ORDER BY $order_by $order";
	}
	
	return select($con, $query);
}

function get_all_paradise($con){
	$query="select * from paradise";
	return select($con,$query);
}

function get_paradise($con, $id)
{
	$query = "SELECT paradise.*, picture.thumb_big FROM paradise, picture " .
		"WHERE paradise.id = '$id' AND (paradise.picture_id = picture.id OR paradise.picture_id IS NULL) LIMIT 1";
	$records = select($con, $query);
	if(count($records) == 1) {
		return $records[0];
	}
	else {
		return NULL;
	}
}

function insert_paradise($con, $paradise)
{
	$name			= $paradise["name"];
	$rating			= $paradise["rating"];
	$type			= $paradise["type"];
	$climate		= $paradise["climate"];
	$climate_high	= $paradise["climate_high"];
	$climate_low	= $paradise["climate_low"];
	$continent		= $paradise["continent"];
	$location_long	= $paradise["location_long"];
	$location_lat	= $paradise["location_lat"];
	$language		= $paradise["language"];
	$religion		= $paradise["religion"];
	$population		= $paradise["population"];
	$gdp			= $paradise["gdp"];
	
	$query = "INSERT INTO paradise(name, rating, type, climate, climate_high, climate_low, " .
		"continent, location_long, location_lat, language, religion, population, gdp) " .
		"VALUES ('$name', '$rating', '$type', '$climate', '$climate_high', '$climate_low', " .
		"'$continent', '$location_long', '$location_lat', '$language', '$religion', '$population', '$gdp')";
	
	return insert($con, $query);
}

function update_paradise($con, $paradise)
{
	$id				= $paradise["id"];
	$name			= $paradise["name"];
	$rating			= $paradise["rating"];
	$type			= $paradise["type"];
	$climate		= $paradise["climate"];
	$climate_high	= $paradise["climate_high"];
	$climate_low	= $paradise["climate_low"];
	$continent		= $paradise["continent"];
	$location_long	= $paradise["location_long"];
	$location_lat	= $paradise["location_lat"];
	$language		= $paradise["language"];
	$religion		= $paradise["religion"];
	$population		= $paradise["population"];
	$gdp			= $paradise["gdp"];
	$picture_id	    = $paradise["picture_id"];
	
	$query = "UPDATE paradise SET name = '$name', rating = '$rating', climate = '$climate', " .
		"climate_high = '$climate_high', climate_low = '$climate_low', continent = '$continent', " .
		"location_long = '$location_long', location_lat = '$location_lat', language = '$language', " .
		"religion = '$religion', population = '$population', gdp = '$gdp', picture_id = '$picture_id' " .
		"WHERE id = '$id'";
	
	return update($con, $query);
}

function get_my_paradise($con){
	session_start();
	$user_id=$_SESSION['user_id'];
	$query= "select * from paradise,user_favorite where user_favorite.user_id=$user_id and paradise.id=user_favorite.paradise_id";
	return select($con,$query);
}
